package com.tianji.learning.listener;

import com.tianji.common.utils.ObjectUtils;
import com.tianji.learning.domain.dto.PointsRecordAddDto;
import com.tianji.learning.service.IPointsRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.tianji.common.constants.MqConstants.Exchange.LEARNING_EXCHANGE;
import static com.tianji.common.constants.MqConstants.Key.*;
import static com.tianji.common.constants.MqConstants.Queue.*;

@Slf4j
@Component
public class PointsRecordListener {
    @Autowired
    private IPointsRecordService pointsRecordService;

    /**
     *  接收签到积分添加消息
     * @param pointsAddMsg 积分人id和积分数量
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = SIGN_POINT_QUEUE,durable = "true"),
            exchange = @Exchange(value = LEARNING_EXCHANGE,type = ExchangeTypes.TOPIC),
            key = SIGN_IN
    ))
    public void listenSignInPoints(PointsRecordAddDto pointsAddMsg){
        log.info("接收到了签到积分的消息：{}",pointsAddMsg);
        if(!ObjectUtils.isAllNotEmpty(pointsAddMsg)){
            log.error("消息参数异常，{}",pointsAddMsg);
            return;
        }

        //添加积分记录
        pointsRecordService.savePointsRecord(pointsAddMsg.getUserId(),2,pointsAddMsg.getPoints());
    }

    /**
     *  接收回复积分添加消息
     * @param userId 积分人id
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = QA_POINT_QUEUE,durable = "true"),
            exchange = @Exchange(value = LEARNING_EXCHANGE,type = ExchangeTypes.TOPIC),
            key = WRITE_REPLY
    ))
    public void listenWriteReplyPoints(Long userId){
        log.info("接收到了回复积分的消息：{}",userId);
        if(!ObjectUtils.isAllNotEmpty(userId)){
            log.error("消息参数异常，{}",userId);
            return;
        }

        //添加积分记录
        pointsRecordService.savePointsRecord(userId,3,5);
    }

    /**
     *  接收学习积分添加消息
     * @param userId 积分人id
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = LEARNING_POINT_QUEUE,durable = "true"),
            exchange = @Exchange(value = LEARNING_EXCHANGE,type = ExchangeTypes.TOPIC),
            key = LEARN_SECTION
    ))
    public void listenLearnSectionPoints(Long userId){
        log.info("接收到了学习积分的消息：{}",userId);
        if(!ObjectUtils.isAllNotEmpty(userId)){
            log.error("消息参数异常，{}",userId);
            return;
        }

        //添加积分记录
        pointsRecordService.savePointsRecord(userId,1,10);
    }
}
